<?= $this->Html->script('ajax-forms', array('inline'=>false)) ?>

<?= $this->Form->create('KalkulatorNalepnica', array(
    'url'   => '/admin_cene/save_nalepnica',
    'class' => 'ajax-form',
    'id'    => 'nalepnica_cene'
)) ?>
    
    <table class="entity-table">
    <tr>
        <th>Primalac</th>
        <th>Broj Računa</th>
        <th>Iznos</th>
        <th></th>
    </tr>
    <? foreach ($data as $i => $row): ?>
    <?
        $key = 'KalkulatorNalepnica';
        $row = $row[$key];
    ?>
    <tr index="<?= $i ?>">
        <?= $this->Form->hidden("{$i}.{$key}.id", array('value' => @$row['id'])) ?>
        <?= $this->Form->hidden("{$i}.{$key}.vrsta_takse", array('value' => 'nalepnica')) ?>
        
        <td><?= $this->Form->text("{$i}.{$key}.company", array('value'=>$row['company'])) ?></td>
        <td><?= $this->Form->text("{$i}.{$key}.account", array('value'=>$row['account'])) ?></td>
        <td><?= $this->Form->text("{$i}.{$key}.value", array('value'=>$row['value'], 'class'=>'number')) ?></td>
        <td class="actions">
            <a href="javascript:void(0)" class="add" onclick="addItem(this)">+</a>
            <a href="javascript:void(0)" class="delete" onclick="deleteItem(this)">-</a>
        </td>
    </tr>
    <? endforeach ?>
    </table>

<?= $this->Form->submit('Sačuvaj') ?>
<?= $this->Form->end() ?><br clear="all">

<script type="text/javascript">
function nalepnica_cene_complete(req) {
    alert(req.responseText);
}
function addItem(a) {
    var tr = $(a).parent().parent();
    var index = parseInt(tr.attr('index'));
    
    if (index >= 0) {
        c = tr.clone();
        index++;
        c.attr('index', index);
        c.find('input').each(function(){
            var name = $(this).attr('name');
            if (name.length) {
                $(this).attr('name', name.replace(/\[\d+\]/i, "["+index+"]"));
            } 
        });
        c.find('input[type=hidden]').eq(0).val('');
        tr.parent().append(c);
    }
}
function deleteItem(a) {
    if (confirm('Da li želite da obrišete cenu?')) {
        $(a).parent().parent().remove();
    }
}
</script>
